package com.teampl.geotrellisspringboot.controller

import geotrellis.raster.Tile
import geotrellis.raster.render.{ColorRamp, ColorRamps, Png}
import geotrellis.spark.{LayerId, SpatialKey}
import geotrellis.spark.io.{Reader, SpatialKeyFormat}
import geotrellis.spark.io.file.FileValueReader
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.{PathVariable, RequestMapping, ResponseBody, RestController}

@RestController
class RenderPngController {
  @RequestMapping(value = Array("{z}/{x}/{y}.png"), produces = Array(MediaType.IMAGE_PNG_VALUE))
  @ResponseBody
  def renderBean(@PathVariable("z") zoom: Int, @PathVariable x: Int, @PathVariable y: Int): Array[Byte] = {
    val fileValueReader = FileValueReader("F:/cabbageShip/项目数据/output")
    val colorRender: ColorRamp = ColorRamps.LightToDarkSunset

    def reader(layerId: LayerId): Reader[SpatialKey, Tile] =
      fileValueReader.reader[SpatialKey, Tile](layerId)

    val tile: Tile = try {
      reader(LayerId("landsat", zoom)).read(x,y)
    }

    val png: Png = tile.renderPng(colorRender)

    png.bytes
  }

}
